package com.example.sqlitetest.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.sqlitetest.Bean.Student;
import com.example.sqlitetest.R;
import com.example.sqlitetest.db.MyDataBaseHelper;
import com.example.sqlitetest.model.StudentInfo;

import org.litepal.crud.DataSupport;

import java.util.List;

import static android.widget.Toast.LENGTH_SHORT;
import static com.example.sqlitetest.Activity.MainActivity.students;
public class ActivityQianDao extends AppCompatActivity {

    String student_num = "";
    private Button qiandao;
    boolean turnoff = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qiandao);
        //初始化数据库对象
        MyDataBaseHelper helper = new MyDataBaseHelper(this);//创建并初始化DBOpenHelper对
        final SQLiteDatabase db = helper.getWritableDatabase();//初始化SQLiteDatabase对象
        student_num = getIntent().getStringExtra("name");
        qiandao = findViewById(R.id.qiandao);
        qiandao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Cursor cursor = db.query("StudentQianDao", null, null, null, null, null, null);

                if (cursor.moveToFirst()) {
                    do {
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        if(name.equals(student_num)){
                                      turnoff = true;
                        }
                    } while (cursor.moveToNext());

                }
                if(turnoff){
                    Toast.makeText(ActivityQianDao.this, "不能重复签到", Toast.LENGTH_SHORT).show();
                }else {
                    ContentValues values = new ContentValues();
                    values.put("name", student_num);
                    values.put("qiandao", "签到成功");
                    db.insert("StudentQianDao", null, values);
                    values.clear();
                    Toast.makeText(ActivityQianDao.this, "签到成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
